package com.njym.java.security.app.rest;


import com.njym.java.security.app.biz.*;
import com.njym.java.security.app.entity.*;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("identityApply")
public class IdentityApplyController extends BaseController<IdentityApplyBiz,IdentityApply> {
    @Autowired
    private FamilyWalletRecordBiz familyWalletRecordBiz;
    @Autowired
    private FamilyUserBiz familyUserBiz;
    @Autowired
    private UserBiz userBiz;
    @Autowired
    private PaymentRecordBiz paymentRecordBiz;

    /**
     * 查看是否能够申请教育专员
     * @param userId
     * @return
     */
    @RequestMapping(value = "/queryIdentity",method = RequestMethod.GET)
    @ResponseBody
    public Object queryIdentity(String userId){
        if(userId != null && userId != "undefined"){
            Integer uId = Integer.parseInt(userId);

            //查询是否购买
            Example example = new Example(PaymentRecord.class);
            example.createCriteria().andEqualTo("lessonId", 55).andEqualTo("chargeUserId", uId).andIsNotNull("tradeNo");
            List<PaymentRecord> record = paymentRecordBiz.selectByExample(example);
            if(record.size() == 0){
                return  ResponseUtil.fail(-4,"尚未购买该课程，请购买学习后再申请!");
            }else{
                //查询是否已申请
                Example example1 = new Example(IdentityApply.class);
                example1.createCriteria().andEqualTo("userId", uId).andNotEqualTo("status", 3);
                List<IdentityApply> apply = baseBiz.selectByExample(example1);
                if(apply.size() > 0){
                    if(apply.get(0).getStatus() == 2){
                        return  ResponseUtil.fail(-2,"申请成功!");
                    }else if(apply.get(0).getStatus() == 3){
                        return  ResponseUtil.fail(-3,"申请未通过，请重新申请!");
                    }else {
                        return ResponseUtil.fail(-1, "正在审核中!");
                    }
                }else{
                    return ResponseUtil.ok();
                }
            }
        }
        else{
            return  ResponseUtil.fail(-1,"网络已中断!");
        }
    }






    @RequestMapping(value = "/apply",method = RequestMethod.GET)
    @ResponseBody
    public Object apply(String name, String mobile, String email, String userId){
        Map<String, Object> data = new HashMap<>(1);
        if(userId != null && userId != "undefined" ) {
            Integer uId = Integer.parseInt(userId);

            IdentityApply identityApply = new IdentityApply();

            identityApply.setStatus(1);
            identityApply.setUserId(uId);
            identityApply.setName(name);
            identityApply.setMobile(mobile);
            identityApply.setEmail(email);

            baseBiz.insert(identityApply);

            return new ObjectRestResponse<IdentityApply>().rel(true).data(data);
        }else {
            return new ObjectRestResponse<IdentityApply>().rel(true).data(data);
        }

    }

    /**
     * 是否是教育专员
     * @param userId
     * @return
     */
    @RequestMapping(value = "/if_commissioner",method = RequestMethod.GET)
    @ResponseBody
    public Object ifCommissioner(String userId){
        if (!userId.equals("undefined")) {
            Example example = new Example(IdentityApply.class);
            example.createCriteria().andEqualTo("userId",Integer.parseInt(userId)).andEqualTo("status",2);
            List<IdentityApply> list = baseBiz.selectByExample(example);
            if(list.size() > 0){    //有教育专员身份
                return new ObjectRestResponse<>().rel(true);
            }else {
                return new ObjectRestResponse<>().rel(false);
            }
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }


}